<?php

/**
 * This is the model class for table "{{user_access}}".
 *
 * The followings are the available columns in table '{{user_access}}':
 * @property integer $id
 * @property integer $userId
 * @property integer $employeeAdmin
 *
 * The followings are the available model relations:
 * @property User $user
 */
class UserAccess extends CActiveRecord {
	/**
	 * Returns the static model of the specified AR class.
	 * @return UserAccess the static model class
	 */
	public static function model($className = __CLASS__) {
		return parent::model($className);
	}

	public static function getActiveRoles() {
		return array(
			'employeeAdmin',
			'materialManage',
			'materialView',
			'materialPriceCheck',
			'projectManage',
			'projectView',
			'materialRequisitionManage',
			'materialRequisitionView',
			'materialWarehouseInboundManage',
			'materialWarehouseInboundView',
			'merchantView',
			'merchantManage',
			'materialReceiptView',
			'materialReceiptManage',
			'accountView',
			'accountManage',
			'accountTokenView',
			'accountTokenManage',
			'accountTokenStat',
			'employeeView',
			'employeeManage',
			'materialExpense',
			'materialViewProject',
			'projectMaterialAnalyze',
			'employeeWorkReportViewProject',
			'employeeWorkReportViewCompany',
			
		);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName() {
		return '{{user_access}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules() {
		$rolesConfig = implode(UserAccess::getActiveRoles(), ', ');
		return array(
			array(
				$rolesConfig,
				'numerical',
				'integerOnly' => true
			),
			array(
				$rolesConfig,
				'numerical',
				'integerOnly' => true
			),
			array(
				$rolesConfig,
				'default',
				'value' => 0
			),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array(
				'userId, ' . $rolesConfig,
				'safe',
				'on' => 'search'
			),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations() {
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'user' => array(
				self::BELONGS_TO,
				'User',
				'userId'
			),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels() {
		return array(
			'id' => 'ID',
			'userId' => '用户',
			'employeeAdmin' => '员工登陆管理',
			'materialManage' => '材料管理',
			'materialView' => '材料查看',
			'materialPriceCheck' => '材料价格查询',
			'projectManage' => '项目管理',
			'projectView' => '项目查看',
			'materialRequisitionManage' => '申购单查看',
			'materialRequisitionView' => '申购单管理',
			'materialWarehouseInboundManage' => '入库单查看',
			'materialWarehouseInboundView' => '入库单管理',
			'merchantView' => '供货商查看',
			'merchantManage' => '供货商管理',
			'materialReceiptView' => '材料三级账查看',
			'materialReceiptManage' => '材料三级账管理',
			'accountView' => '收付账查看',
			'accountManage' => '收付账管理',
			'accountTokenView' => '帐目凭证查看',
			'accountTokenManage' => '帐目凭证管理',
			'accountTokenStat' => '账目凭证报表',
			'employeeView' => '员工查看',
			'employeeManage' => '员工管理',
			'materialExpense' => '材料费用报销',
			'materialViewProject' => '材料使用查询',
			'projectMaterialAnalyze' =>'项目材料分析',
			'employeeWorkReportViewProject' => '查看所在项目工作汇报',
			'employeeWorkReportViewCompany' => '查看公司工作汇报',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search() {
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria = new CDbCriteria;

		$criteria->compare('user.username', $this->userId, true);
		foreach (UserAccess::getActiveRoles() as $role) {
			$criteria->compare($role, $this->$role);
		}

		$criteria->with = array(
			'user'
		);

		return new CActiveDataProvider($this, array(
			'criteria' => $criteria,
		));
	}

}
